package com.fingard.dsp.bank.directbank.bocom12;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.directbank.bocom13.util.MD5;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.GZipHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 交通银行资金清算平台批量代收付
 * 
 * 参考文档：
 * 		1、交通银行资金清算平台批量数据传送接口V1.0.doc
 * 		2、交通银行资金清算平台数据字典V1.0.doc
 * 		3、交通银行资金清算平台联机交易接口V1.0.doc
 * 
 * @author Cruise
 * @date 2019/4/18
 */
public class BOCOM12BatPayRec extends BOCOM12Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
		return d_BatPayRec(p_req1988);
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
		return d_QryBatPayRec(p_req1998);
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		return d_BatPayRec(p_req9188);
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		return d_QryBatPayRec(p_req9198);
	}

	/**
	 * 开发接口：
	 * 		1、4.1.1	批量代收/代付文件
	 * 		2、3.2.1	【报文1】结构说明
	 * 		3、3.2.2	【报文2】结构说明
	 * 		4、3.2.3	【报文3】结构说明
	 * 		5、3.2.4	【报文4】结构说明
	 * 		6、3.2.5	【报文5】结构说明
	 * 		7、5.1	文件确认（B60911/I60911）
	 * 
	 * @param p_reqBat
	 * @return
	 * @throws Exception
	 */
	public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		retAts.reqSeqID = p_reqBat.reqBatchNO;

		FileInputStream fis = null;
		
		try {
			String tmpPayDate = Format.Date8Format.format(p_reqBat.getPayDateTime());
			
			String bankBranch = getBankConfig().getMapValueByKey("bankBranch", p_reqBat.ownAct);
			if (StringHelper.isNullOrEmpty(bankBranch)) {
				retAts.respCode = "0";
				retAts.respInfo = "未配置账号[" + p_reqBat.ownAct + "]银行方分支机构映射,请检查config.xml中'bankBranch'配置项";
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
				retAts.writeRetToATS();
				return retAts;
			}

			String fileType = "";
			if (p_reqBat.reqHeader.isBatRec) {
				fileType = BOCOM12TransCode.batRec_9188_918801;
			} else {
				fileType = BOCOM12TransCode.batPay_1988_198801;
			}
			
			String tmpBatSeqNo = getBatSeqNo(getTempFileDirectory(), 5);
			
			String tmpUploadFileName = "I_B_" + tmpPayDate + "_" + bankFront.corpNo + "_" + bankBranch + "_" + fileType + ".S." + tmpBatSeqNo;
			String tmpLocalFilePath = getTempFileDirectory() + File.separator + tmpUploadFileName;
			String tmpGZipFilePath = tmpLocalFilePath + ".gz";

			byte tmpByteSp = 0x0F;// 域分隔符
			byte tmpByteLn = (byte) '\n';// 换行符

			StringBuilder sbFileContent = new StringBuilder();

			sbFileContent.append("3012900");/* 银行方ID:(3012900:交通银行) */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(bankBranch);/* 银行分行代码:保险公司发起方账号开户分行代码 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(tmpByteSp);/* 应答类型 */
			sbFileContent.append(tmpByteSp);/* 错误码 */
			sbFileContent.append(tmpByteSp);/* 错误说明 */
			sbFileContent.append(tmpByteSp);/* 银行方业务日期 */
			sbFileContent.append(tmpByteSp);/* 银行方交易日期 */
			sbFileContent.append(tmpByteSp);/* 银行方交易时间 */
			sbFileContent.append(tmpByteSp);/* 银行方业务日期 */
			sbFileContent.append(tmpByteSp);/* 银行方交易日期 */
			sbFileContent.append(tmpByteSp);/* 银行方交易时间 */
			sbFileContent.append(tmpByteSp);/* 银行方批次号 */

			sbFileContent.append(bankFront.corpNo);/*保险方ID: 保险公司编码 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(tmpPayDate);/* 保险方业务日期(yyyyMMdd) */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(tmpPayDate);/* 保险方交易日期(yyyyMMdd) */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(Format.Time6Format.format(p_reqBat.getPayDateTime()));/* 保险方交易时间 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(p_reqBat.reqBatchNO);/* 保险方批次号 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(p_reqBat.totalNum);/* 总笔数 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(p_reqBat.totalAmt);/* 总金额 */
			sbFileContent.append(tmpByteSp);

			sbFileContent.append(tmpByteSp);/* 成功笔数 */
			sbFileContent.append(tmpByteSp);/* 成功金额 */
			sbFileContent.append(tmpByteSp);/* 预留字段01 */
			sbFileContent.append(tmpByteSp);/* 预留字段02 */
			sbFileContent.append(tmpByteSp);/* 预留字段03 */
			sbFileContent.append(tmpByteSp);/* 预留字段04 */
			sbFileContent.append(tmpByteSp);/* 预留字段05 */
			sbFileContent.append(tmpByteSp);/* 预留字段06 */
			sbFileContent.append(tmpByteSp);/* 预留字段07 */
			sbFileContent.append(tmpByteSp);/* 预留字段08 */
			sbFileContent.append(tmpByteSp);/* 预留字段09 */
											/* 预留字段10 */

			int tmpRdCount = 0;
			while (p_reqBat.readRecord()) {
				tmpRdCount++;
				sbFileContent.append(tmpByteLn);

				sbFileContent.append(tmpRdCount);/* 记录序号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(tmpByteSp);/* 应答类型 */
				sbFileContent.append(tmpByteSp);/* 错误码 */
				sbFileContent.append(tmpByteSp);/* 错误说明 */
				sbFileContent.append(tmpByteSp);/* 银行方业务日期 */
				sbFileContent.append(tmpByteSp);/* 银行方流水号 */

				sbFileContent.append(tmpPayDate);/* 保险方业务日期 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.askNumber);/* 保险方流水号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.insuPolicyNum);/* 投保单号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.sourceNoteCode);/* 保单号 */
				sbFileContent.append(tmpByteSp);

				String oppName = "";
				if (p_reqBat.reqHeader.isBatRec) {
					oppName = p_reqBat.readedDetail.payName;
				} else {
					oppName = p_reqBat.readedDetail.recName;
				}
				sbFileContent.append(oppName);/* 客户姓名 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(convertCertTypeToBank(p_reqBat.readedDetail.certType));/* 证件类型 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.certNum);/* 证件号码 */
				sbFileContent.append(tmpByteSp);

				String isPrivate = "";
				if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
					isPrivate = "P";
				} else {
					isPrivate = "E";
				}
				sbFileContent.append(isPrivate);/* 公私标志(E-对公,P-对私) */
				sbFileContent.append(tmpByteSp);

				String sameBnk = "";
				if ("1".equals(p_reqBat.readedDetail.sameBnk)) {
					sameBnk = "1";
				} else {
					sameBnk = "2";
				}
				sbFileContent.append(sameBnk);/* 银行标志(1-交通银行 2-非交通银行) */
				sbFileContent.append(tmpByteSp);

				String cnapsCode = "";
				if ("0".equals(p_reqBat.readedDetail.sameBnk)) {
					cnapsCode = p_reqBat.readedDetail.cnapsCode;
				}
				sbFileContent.append(cnapsCode);/* 收款行行号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(tmpByteSp);/* 险种名称 */

				String oppAct = "";
				if (p_reqBat.reqHeader.isBatRec) {
					oppAct = p_reqBat.readedDetail.payAct;
				} else {
					oppAct = p_reqBat.readedDetail.recAct;
				}
				sbFileContent.append(oppAct);/* 账号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append("CNY");/* 交易币种 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.payAmount);/* 交易金额 */
				sbFileContent.append(tmpByteSp);

				String batBusiType = "";
				if (p_reqBat.reqHeader.isBatRec) {
					batBusiType = getBankConfig().getSetValue("businessType", "Memo", "9188");
					if (StringHelper.hasAnyChar(batBusiType)) {
						batBusiType = getBankConfig().getMapValueByKey("recBusiCode", p_reqBat.readedDetail.srcXElement.getSingleText(batBusiType));
					} else {
						batBusiType = "D30";// 默认值:续期缴费
					}
				} else {
					batBusiType = getBankConfig().getSetValue("businessType", "Usage", "1988");
					if (StringHelper.hasAnyChar(batBusiType)) {
						batBusiType = getBankConfig().getMapValueByKey("payBusiCode", p_reqBat.readedDetail.srcXElement.getSingleText(batBusiType));
					} else {
						batBusiType = "C30";// 默认值:保险理赔
					}
				}
				sbFileContent.append(batBusiType);/* 收付种类 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.cellPhone);/* 手机号码 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(p_reqBat.readedDetail.authCode);/* 代收协议编号 */
				sbFileContent.append(tmpByteSp);

				sbFileContent.append(tmpByteSp);/* 预留字段01 */
				sbFileContent.append(tmpByteSp);/* 预留字段02 */
				sbFileContent.append(tmpByteSp);/* 预留字段03 */
				sbFileContent.append(tmpByteSp);/* 预留字段04 */
				sbFileContent.append(tmpByteSp);/* 预留字段05 */
				sbFileContent.append(tmpByteSp);/* 预留字段06 */
				sbFileContent.append(tmpByteSp);/* 预留字段07 */
				sbFileContent.append(tmpByteSp);/* 预留字段08 */
				sbFileContent.append(tmpByteSp);/* 预留字段09 */
												/* 预留字段10 */
			}

			WriteBankLogLn2("批量代收付请求文件内容:\r\n" + sbFileContent.toString());
			WriteBankLogLn("待压缩批量代收付请求文件保存本地路径:" + tmpLocalFilePath);
			FileHelper.writeFile(tmpLocalFilePath, sbFileContent.toString(), getCharset());

			GZipHelper.doCompressFile(tmpLocalFilePath, tmpGZipFilePath);
			WriteBankLogLn("压缩后批量代收付请求文件保存本地路径：" + tmpGZipFilePath);

			// 2.文件传输
			// 2.1 报文1
			StringBuilder tmpSbPck1 = new StringBuilder();
			tmpSbPck1.append("I60901");/* 交易码 */
			tmpSbPck1.append("I");/* 文件创建方标识 */
			tmpSbPck1.append("B");/* 文件接收方标识 */
			tmpSbPck1.append(String.format("%-15s", bankFront.corpNo));/* 保险方ID */
			tmpSbPck1.append(String.format("%-10s", "3012900"));/* 银行方ID */
			tmpSbPck1.append(String.format("%-20s", bankBranch));/* 银行分行代码 */
			tmpSbPck1.append(tmpPayDate);/* 业务日期 */
			tmpSbPck1.append(String.format("%-20s", p_reqBat.reqBatchNO));/* 交易流水号 */
			tmpSbPck1.append(tmpPayDate);/* 交易日期 */
			tmpSbPck1.append(Format.Time6Format.format(p_reqBat.getPayDateTime()));/* 交易时间 */
			tmpSbPck1.append(String.format("%-128s", tmpUploadFileName + ".gz"));/* 压缩文件名称 */
			tmpSbPck1.append(String.format("%012d", new File(tmpGZipFilePath).length()));/* 文件大小 */
			int maxBlockSize = 2097152;
			int tmpBlockSize = (int) new File(tmpGZipFilePath).length();
			if (tmpBlockSize > maxBlockSize) {
				tmpBlockSize = maxBlockSize;
			}
			tmpSbPck1.append(String.format("%08d", tmpBlockSize));/* 数据包大小 */
			String tmpGZMD5 = MD5.generateFileMD5(tmpGZipFilePath);
			tmpSbPck1.append(String.format("%-200s", tmpGZMD5));/* 文件MD5校验值 */
			tmpSbPck1.append(String.format("%-1500s", ""));

			boolean tmpFinished = false;
			String tmpFileInfoCode = "";
			String tmpFileMsg = "";

			int tmpStep = 1;
			int tmpReadedLen = 0;
			byte[] tmpLastBuffer = new byte[0];
			int tmpLastBufLength = 0;
			byte[] tmpNowBuffer = new byte[0];
			int tmpNowBufLength = 0;
			int tmpLastSeq = -1;
			String tmpBankAskSeq = "";// 数据包序号

			String[] tmpBkRet = new String[] { "", "" };
			while (tmpReadedLen <= new File(tmpGZipFilePath).length()) {
				if (tmpStep == 1) {
					WriteBankLogLn("发送银行的【报文1】请求数据:\r\n" + tmpSbPck1);
					tmpBkRet = sendToBank(tmpSbPck1.toString(), bankFront.filePort);
					tmpStep++;
				} else {
					byte[] bytesBody3 = ("I60902" + tmpBankAskSeq).getBytes(getCharset());
					WriteBankLogLn("发送银行的报文3请求数据，不包含数据包为:" + "I60902" + tmpBankAskSeq);
					byte[] bytesP3ToSend = new byte[bytesBody3.length + tmpNowBufLength];
					System.arraycopy(bytesBody3, 0, bytesP3ToSend, 0, bytesBody3.length);
					System.arraycopy(tmpNowBuffer, 0, bytesP3ToSend, bytesBody3.length, tmpNowBufLength);
					tmpBkRet = sendToBank(new String(bytesP3ToSend));
					tmpLastBuffer = tmpNowBuffer;
					tmpLastBufLength = tmpNowBufLength;
					tmpLastSeq = Integer.parseInt(tmpBankAskSeq);
				}
				tmpBankAskSeq = "";
				tmpNowBuffer = new byte[0];
				tmpNowBufLength = 0;

				if (tmpBkRet[0].length() == 0) {
					byte[] bytesBkRet = tmpBkRet[1].getBytes(getCharset());
					if (bytesBkRet.length > 24) {
						tmpBankAskSeq = new String(bytesBkRet, 25, 6);
						byte tmpRespType = bytesBkRet[24];
						if (tmpRespType == 'G') {
							WriteBankLogLn("【报文2】响应数据中数据包序号：" + tmpBankAskSeq);
							int tmpAskSeq = Integer.parseInt(tmpBankAskSeq);
							int expectedSeq = tmpLastSeq + 1;
							if (tmpAskSeq == expectedSeq) {// 继续读取文件
								tmpNowBuffer = new byte[maxBlockSize];
								
								fis = new FileInputStream(new File(tmpGZipFilePath));
								byte[] bytes = new byte[maxBlockSize];
								fis.read(bytes);
								tmpNowBufLength = bytes.length;
								if (tmpNowBufLength > 0) {
									tmpReadedLen += tmpNowBufLength;
								} else {
									tmpFinished = false;
									tmpFileMsg = "数据包序号" + tmpAskSeq + "已无数据可读";
									break;
								}
							} else {
								tmpFinished = false;
								tmpFileMsg = "数据包序号" + tmpAskSeq + "与预期序号" + expectedSeq + "不符";
								break;
							}
						} else if (tmpRespType == 'R') {
							WriteBankLogLn("3.【报文2】请求重传数据包序号：" + tmpBankAskSeq);
							int tmpReSendSeq = Integer.parseInt(tmpBankAskSeq);
							if (tmpReSendSeq == tmpLastSeq) {// 继续读取文件
								tmpNowBuffer = tmpLastBuffer;
								tmpNowBufLength = tmpLastBufLength;
							} else {
								tmpFinished = false;
								tmpFileMsg = "重传数据包序号" + tmpReSendSeq + "与上次序号" + tmpLastSeq + "不符";
								break;
							}
						} else if (tmpRespType == 'N') {
							WriteBankLogLn("3.【报文4】N-文件传输成功：" + tmpBankAskSeq);
							tmpFinished = true;
							break;
						} else if (tmpRespType == 'E') {
							WriteBankLogLn("3.【报文5】E-文件传输失败：" + tmpBankAskSeq);
							tmpFinished = false;
							tmpFileInfoCode = tmpBankAskSeq;
							tmpFileMsg = new String(bytesBkRet, 31, 300).trim();
							break;
						} else if (tmpRespType == 'A') {
							WriteBankLogLn("【报文5】A-文件传输异常：" + tmpBankAskSeq);
							tmpFinished = false;
							tmpFileInfoCode = tmpBankAskSeq;
							tmpFileMsg = new String(bytesBkRet, 31, 300).trim();
							break;
						} else {
							WriteBankLogLn("3.【报文2】不能识别的应答类型：" + (char) bytesBkRet[24]);
							tmpFinished = false;
							tmpFileMsg = "不能识别的应答类型：" + (char) bytesBkRet[24];
							break;
						}
					}
				} else {
					tmpFinished = false;
					tmpFileInfoCode = "E9999";
					tmpFileMsg = tmpBkRet[1];
					break;
				}
			}
			
			if (tmpFinished) {
				// 发起文件确认(B60911/I60911)
				WriteBankLogLn("2.文件发送成功，进行文件确认，连接服务：" + bankFront.serverIP + ":" + bankFront.serverPort);
				
				BOCOM12Header tmpBankHeader = new BOCOM12Header();
				tmpBankHeader.transCode = "I60911";
				tmpBankHeader.reqSeqID = p_reqBat.reqBatchNO;
				
				XmlTextWriter xmlWriter = new XmlTextWriter();
				writeCommonHeader(xmlWriter, tmpBankHeader);

				xmlWriter.writeStartElement("Body");
				xmlWriter.writeElementString("FileName", tmpUploadFileName + ".gz");/* 压缩文件名称 */
				xmlWriter.writeElementString("TotalNum", p_reqBat.totalNum);/* 总笔数 */
				xmlWriter.writeElementString("TotalAmt", p_reqBat.totalAmt);/* 总金额 */
				xmlWriter.writeElementString("BatNo", p_reqBat.reqBatchNO);/* 创建方批次号 */
				xmlWriter.writeEndDocument();

				String[] tmpRetStr = sendToBank(xmlWriter.toXmlString());
				
				if (tmpRetStr[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
					if (tmpBkReader != null) {
						retAts.respCode = "0";
						retAts.respInfo = tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
						String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
						String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
						retAts.transState = tmpMsgType + "+" + tmpErrCode;
						retAts.payInfoCode = tmpErrCode;
					} else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpRetStr[1];
				}
			} else {
				retAts.respCode = "0";
				retAts.respInfo = tmpFileMsg;
				retAts.transState = "BatchFailed";
				retAts.payInfoCode = tmpFileInfoCode;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.toString();
			WriteBankLogLn(ex);
		} finally {
			if (fis != null) {
				fis.close();
			}
		}

		retAts.writeRetToATS();
		return retAts;
	}

	public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		retAts.reqSeqID = p_reqQryBat.reqBatchNO;

		try {
			String tmpNotifyDataPath = getNotifyDataDirectory() + File.separator + p_reqQryBat.reqBatchNO + ".txt";
			WriteBankLogLn("银行结果文件路径:" + tmpNotifyDataPath);
			
			if (new File(tmpNotifyDataPath).exists()) {
				String tmpStrRet = FileHelper.readFileText(tmpNotifyDataPath, getCharset());
				WriteBankLogLn("银行结果文件内容:\r\n" + tmpStrRet);
				StringReader strReader = new StringReader(tmpStrRet);
				BufferedReader tmpReader = new BufferedReader(strReader);
				String[] line1 = tmpReader.readLine().split(",");
				if (line1.length > 16) {
					String tmpBkBatNO = line1[16];
					if (p_reqQryBat.reqBatchNO.equals(tmpBkBatNO)) {
						retAts.respCode = "0";
						retAts.respInfo = line1[14];
						String tmpBatInfo = line1[4];
						String tmpBatState = line1[2];
						String tmpBatInfoCode = line1[3];
						retAts.transState = tmpBatState + "+" + tmpBatInfoCode;
						retAts.payInfoCode = tmpBatInfoCode;
						while(true) {
							String[] eachLine = tmpReader.readLine().split(",");
							if (eachLine.length > 16) {
								RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

								tmpRetDetail.postScript = eachLine[7];
								if ("1998".equals(p_reqQryBat.reqHeader.transType)) {
									tmpRetDetail.recAct = eachLine[17];
								} else {
									tmpRetDetail.payAct = eachLine[17];
								}
								
								tmpRetDetail.payAmount = eachLine[19];
								 String tmpResult = "";
                                 String tmpBkDtState = eachLine[1];
                                 String tmpBkDtInfoCode = eachLine[2];
                                 String tmpBkDtInfo = eachLine[3];
                                 if(tmpBkDtState.length()==0&&tmpBkDtInfoCode.length()==0&&tmpBkDtInfo.length()==0&&"E".equals(tmpBkDtState)&&tmpBkDtInfoCode!="000000") {
                                	 tmpResult = tmpBatState + "+" + tmpBatInfoCode;
                                     tmpBkDtInfoCode = tmpBatInfoCode;
                                     tmpBkDtInfo = tmpBatInfo;
                                 } else {
                                	 tmpResult = "DT+" + tmpBkDtState + "+" + tmpBkDtInfoCode;
                                 }
                                 
								tmpRetDetail.memo = tmpBkDtInfo;
								tmpRetDetail.result = tmpResult;
								tmpRetDetail.payInfoCode = tmpBkDtInfoCode;

								retAts.writeRetDetail(tmpRetDetail);
							} else {
								break;
							}
						}
					} else {
						retAts.respCode = "-1";
						retAts.respInfo = "结果文件中的批次号" + tmpBkBatNO + "与请求批次号" + p_reqQryBat.reqBatchNO + "不一致";
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "结果文件格式不符合预期，首行字段数不足17个";
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "当前未收到交行的结果文件";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}
}
